package inc.gome.dynamic.datasource.netty;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ConcurrentHashMap;

public class ResponseMappingCallback {
    static ConcurrentHashMap<Long, CompletableFuture> mapping = new ConcurrentHashMap<>();

    public static void  addCallBack(long requestID,CompletableFuture cb){
        mapping.putIfAbsent(requestID,cb);
    }


    //触发回调
    public static void runCallBack(ResponseBody msg){
        CompletableFuture cf = mapping.get(msg.getRequestId());
//        runnable.run();
        System.out.println("获取响应回调 =====>"+cf);
        cf.complete(msg.getBody());
        removeCB(msg.getRequestId());
    }

    private static void removeCB(long requestID) {
        mapping.remove(requestID);
    }

}